package com.sw.study.ssm.controller;

import com.sw.study.ssm.pojo.Resume;
import com.sw.study.ssm.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * Created by sw on 2020/4/18.
 * 后台模拟数据
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;

    @RequestMapping("/list")
    public ModelAndView list(){
        List<Resume> resumes = resumeService.queryList();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("resume");
        modelAndView.addObject("resumes",resumes);
        return modelAndView;
    }
    // 新增
    @RequestMapping("add")
    public String add(HttpServletRequest request, HttpServletResponse response) {

        Resume resume = new Resume();
        resume.setName("addUserName:"+Thread.currentThread().getName().subSequence(0,10));
        resume.setAddress("addUserAddress:"+System.currentTimeMillis());
        resume.setPhone(UUID.randomUUID().toString().substring(0,11));

        resumeService.inserResume(resume);
        // 重定向到登录页面
        return "redirect:/resume/list";
    }


    @RequestMapping("update")
    public String update(HttpServletRequest request, HttpServletResponse response, Integer id) {

        Resume resume = new Resume();
        resume.setId(new Long(id));
        resume.setName("modifyUser:"+Thread.currentThread().getName().subSequence(0,10));
        resume.setAddress("modidyaddress:"+System.currentTimeMillis());
        resume.setPhone(UUID.randomUUID().toString().substring(0,11));

        resumeService.inserResume(resume);
        // 重定向到登录页面
        return "redirect:/resume/list";
    }
    @RequestMapping("delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, Long id) {

        resumeService.deleteResumeById(id);
        // 重定向到登录页面
        return "redirect:/resume/list";
    }
}